
import { defineStore } from 'pinia'

export const useTimer = defineStore('timer', {
  state: () => ({
    quesTime: 0,
    quesTimer: null,
    paperTime: 0,
    paperTimer: null,
  }),
  actions: {

    // 答题计时器开始
    quesTimeStart() {
      this.quesTime = 0
      if(this.quesTimer) clearInterval(this.quesTimer)
      this.quesTimer = setInterval(() => {
        this.quesTime++
        // console.log('答题计时器开始->', this.quesTime);
      }, 1000)
    },

    // 答题计时器停止
    quesTimeEnd() {
      this.quesTime = 0
      this.quesTimer = null
    },

    // 试卷计时器开始
    paperTimeStart(time = 0) {
      this.paperTime = time
      if(this.paperTimer) clearInterval(this.paperTimer)
      this.paperTimer = setInterval(() => {
        this.paperTime++
        // console.log('试卷计时器开始->', this.quesTime);
      }, 1000)
    },

    // 答题计时器停止
    paperTimeEnd() {
      this.paperTime = 0
      this.paperTimer = null
    },
  },
  persist: {
    storage: piniaPluginPersistedstate.localStorage(),
  },
})
